package cn.jilian.admin.tool.web;

import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.jilian.admin.tool.domain.ColumnInfo;
import cn.jilian.admin.tool.domain.PageAndSort;
import cn.jilian.admin.tool.domain.TableInfo;
import cn.jilian.admin.tool.service.DBService;
import cn.jilian.common.domain.R;
import cn.jilian.common.dto.PageResult;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/db")
public class DBController {
	
	@Autowired
	private DBService dbService;
	
	@GetMapping("/list.html")
	public ModelAndView tableList() {
		ModelAndView mv=new ModelAndView("database/table");
		return mv;
	}
	
	@GetMapping("/column.html")
	public ModelAndView columns(@RequestParam(name="table")String name) {
		ModelAndView mv=new ModelAndView("database/column");
		mv.addObject("table", name);
		return mv;
	}
	@ApiOperation(value = "新建数据表", notes = "")
	@RequestMapping(value="/addTable",method= {RequestMethod.GET,RequestMethod.POST})
	public R addTable(@RequestParam(name="table")String table,@RequestParam(name="comment")String comment) {
		try {
			dbService.createTable(table, comment);
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	@ApiOperation(value = "分页查询数据库表", notes = "")
	@RequestMapping(value = "/list", method = RequestMethod.POST) 
	@ResponseBody
	public PageResult<Map<String,Object>> tableList(@RequestBody PageAndSort ps){
		return dbService.queryTableList(ps.getPageInfo(),ps.getSortInfo());
	}
	
	@ApiOperation(value = "分页查询表列", notes = "")
	@PostMapping("/table/columns/{table}")
	public PageResult<Map<String,Object>> columns(@PathVariable("table")String table,@RequestBody PageAndSort ps){
		
		return dbService.queryTableColumns(ps.getPageInfo(), table);
	}
	
	@ApiOperation(value = "新增表列", notes = "")
	@PostMapping("/column/add")
	public R addColumn(ColumnInfo columnInfo) {
		int r=dbService.addColumn(columnInfo);
		if(r>0) {
			return R.ok("新增成功");
		}else {
			return R.fail;
		}
		
	}
	@ApiOperation(value = "导出insert语句", notes = "返回对应的文件流")
	@GetMapping("/table/export/insertsql")
	public ResponseEntity<byte[]> exportInsertSql(@RequestParam("table")String table){
		String sql=dbService.exportInsertSql(table);
		byte[] body=sql.getBytes(Charset.forName("UTF-8"));
		HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename="+table+"-data.sql");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
	}
	
	@ApiOperation(value = "导出建表语句", notes = "返回对应的文件流")
	@GetMapping("/table/export/createsql")
	public ResponseEntity<byte[]> exportCreateSql(@RequestParam("table")String table){
		String sql=dbService.exportCreateSql(table);
		byte[] body=sql.getBytes(Charset.forName("UTF-8"));
		HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename="+table+"-schema.sql");
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
	}
	
	@ApiOperation(value = "更新表定义", notes = "")
	@GetMapping("/table/update")
	public R updateTable(@RequestParam("table")String table,TableInfo info) {
		try {
			if(info!=null) {
				dbService.updateTableComment(table,info.getName(), info.getComment());
			}
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	@ApiOperation(value = "更新表列定义", notes = "")
	@PostMapping("/table/column/update")
	public R updateColumn(ColumnInfo columnInfo) {
		try {
			dbService.updateColumn(columnInfo);
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	
	@ApiOperation(value = "更新表列排序", notes = "")
	@PostMapping("/column/move")
	public R moveColumn(@RequestParam("table")String table,@RequestParam("column")String column,@RequestParam("step")Integer step) {
		try {
			dbService.moveSeq(table, column, step);
			return R.ok;
		}catch(Exception e) {
			e.printStackTrace();
			return R.fail;
		}
	}
	@ApiOperation(value = "彻底删除表", notes = "")
	@RequestMapping(value="/table/delete",method= {RequestMethod.GET,RequestMethod.POST})
	public R deleteTable(@RequestParam("tables")Collection<String> tables) {
		try {
			dbService.deleteTables(tables);
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}
	@ApiOperation(value = "删除表列", notes = "")
	@GetMapping("/column/delete")
	public R deleteTableColumn(@RequestParam("table")String table,@RequestParam("columns")Collection<String> columns) {
		try {
			dbService.deleteColumns(table, columns);
			return R.ok;
		}catch(Exception e) {
			return R.fail;
		}
	}

}
